package 网络编程;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class TCP_ServerDemo2 {
    public static void main(String[] args) throws IOException {
        //TCP协议,接收数据
        //1.创建socket对象
        ServerSocket serverSocket=new ServerSocket(10002);
        //监听客户端连接
        Socket accept = serverSocket.accept();
        //2.从连接中获取输入流
        InputStream inputStream = accept.getInputStream();
        //使用转换流将字节转成字符流,使得中文也可以输出
        InputStreamReader isr=new InputStreamReader(inputStream);
        /*byte[] bytes = inputStream.readAllBytes();
        for (byte aByte : bytes) {
            System.out.println((char) aByte);
        }*/
        int b;
        while ((b=isr.read())!=-1){
            System.out.print((char)b);
        }

        //释放资源
        isr.close();
        serverSocket.close();
    }
}
